from django.db import models
from utils.modelsMixin import BaseSetModel
from django.contrib.auth.models import User


# Create your models here.
# 用户详情模型
class UserDetail(BaseSetModel):
    # 性别选择集合
    SEX_CHOICES = ((1, '男'), (0, '女'))
    # 头像
    avatar = models.TextField(null=True, blank=True, verbose_name='头像')
    # 性别
    sex = models.IntegerField(null=True, blank=True, choices=SEX_CHOICES, verbose_name='性别')
    # 生日
    birthday = models.DateField(null=True, blank=True, verbose_name='生日')
    # 手机号码
    phone = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机号码')
    # 外键字段和作者关联
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    # 设置表名
    class Meta:
        db_table = 'user_detail'


# 地址省市区模型
class Area(models.Model):
    # 地点名称
    name = models.CharField(max_length=20, verbose_name='地点名称')
    # 父级地点
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='上级行政区')

    # 设置表明
    class Meta:
        db_table = 'area'

    # 书写魔法方法,返回名字
    def __str__(self):
        return self.name


# 地址详情模型
class Address(BaseSetModel):
    # 地址名
    name = models.CharField(max_length=40, verbose_name='地址名')
    # 收货人
    receiver = models.CharField(max_length=40, verbose_name='收货人')
    # 手机号联系方式
    mobile = models.CharField(max_length=11, verbose_name='手机号联系方式')
    # 省
    province = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='省', related_name='province_address')
    # 市
    city = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='市', related_name='city_address')
    # 区
    district = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='区', related_name='district_address')
    # 详细地址
    place = models.CharField(max_length=40, verbose_name='详细地址')
    # 关联用户
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    # 重新设置表名
    class Meta:
        # 设置排序顺序
        ordering = ['-update_time']
        db_table = 'address'

    # 重写魔法方法,返回地址名
    def __str__(self):
        return self.name

    # 增加delete方法，只做逻辑删除,并不是真的删除
    def delete(self):
        self.is_delete = True
        self.save()
